/*
 ============================================================================
 Name		: FETimer.cpp
 Author	  : shangguan
 Version	 : 1.0
 Copyright   : this calass for file open and save
 Description : CFETimer implementation
 ============================================================================
 */

#include "FETimer.h"
#include "FEAutoSave.h"

CFETimer::CFETimer(MFEAutoSave &aSave) :
	CActive(EPriorityStandard), iAS(aSave) // Standard priority
{
}

CFETimer* CFETimer::NewLC(MFEAutoSave &aSave)
{
	CFETimer* self = new (ELeave) CFETimer(aSave);
	CleanupStack::PushL(self);
	self->ConstructL();
	return self;
}

CFETimer* CFETimer::NewL(MFEAutoSave &aSave)
{
	CFETimer* self = CFETimer::NewLC(aSave);
	CleanupStack::Pop(); // self;
	return self;
}

void CFETimer::ConstructL()
{
	User::LeaveIfError(iTimer.CreateLocal()); // Initialize timer
	CActiveScheduler::Add(this); // Add to scheduler
}

CFETimer::~CFETimer()
{
	Cancel(); // Cancel any request, if outstanding
	iTimer.Close(); // Destroy the RTimer object
}

void CFETimer::DoCancel()
{
	iTimer.Cancel();
}

void CFETimer::Stop()
{
	DoCancel();
}
/*******************************************************************************
 * @Name:       StartL
 * @Author:     shangguan
 * @Purpose:    start a CActive object 
 * @Returns:    void
 * @Parameters:	aDelay--the time need to delay
 * @Remarks:    -
 ******************************************************************************/
void CFETimer::StartL(TTimeIntervalMicroSeconds32 aDelay)
{
	// Cancel any request, just to be sure
	Cancel();
	// Set for later
	iTimer.After(iStatus, aDelay);
	// Tell scheduler a request is active
	SetActive();
}
/*******************************************************************************
 * @Name:       RunL 
 * @Author:     shangguan
 * @Purpose:    The RunL function of a CActive.Use for autosave file
 * @Returns:    void
 * @Parameters:	void
 * @Remarks:    -
 ******************************************************************************/
void CFETimer::RunL()
{
	iAS.AutoSaveL();//save to file
	iTimer.After(iStatus, 30000000); // Set for 30 sec later
	SetActive(); // Tell scheduler a request is active
}

TInt CFETimer::RunError(TInt aError)
{
	return aError;
}
